package com.sun.androidexerciseproject.function.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Proxy class usage
 *
 * @author steven
 * @time 2017/2/28
 * @Coptright none
 */

public class ProxySubject implements InvocationHandler {
    protected Subject mSubject;

    protected ProxySubject(Subject subject) {
        mSubject = subject;
    }

    public static void main(String[] args) {
        Subject subject = new RealSubject();
        ProxySubject proxy = new ProxySubject(subject);
        Subject sub = (Subject) Proxy.newProxyInstance(subject.getClass().getClassLoader(),
                subject.getClass().getInterfaces(), proxy);
        System.out.println(sub.operation());
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(mSubject, args);
    }
}
